package com.njworkorder.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.njworkorder.DTO.ComponentsAddDto;
import com.njworkorder.DTO.PartsDto;
import com.njworkorder.Entity.Components;
import com.njworkorder.Entity.ModelNum;
import com.njworkorder.Entity.Parts;
import com.njworkorder.Mapper.ModelNumMapper;
import com.njworkorder.Mapper.PartsMapper;
import com.njworkorder.Service.ComponentsService;
import com.njworkorder.Utils.Result;
import com.njworkorder.Utils.ResultUtil;
import com.njworkorder.VO.ComponentsListVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "设施部件管理", description = "部件管理、零部件、型号等相关接口")
@RestController
@RequestMapping("/components")
public class ComponentsController {

    private final ModelNumMapper modelNumMapper;
    private final PartsMapper partsMapper;
    private final ComponentsService componentsService;

    public ComponentsController(ComponentsService componentsService,
                                ModelNumMapper modelNumMapper,
                                PartsMapper partsMapper) {
        this.componentsService = componentsService;
        this.modelNumMapper = modelNumMapper;
        this.partsMapper = partsMapper;
    }

    @Operation(summary = "添加设施部件信息")
    @PostMapping("/addInfo")
    public Result<String> addInfo(@RequestBody ComponentsAddDto componentsDto) {
        Components components = new Components();
        BeanUtils.copyProperties(componentsDto, components);
        boolean save = componentsService.save(components);
        if (save) {
            return ResultUtil.success("添加成功");
        }
        return ResultUtil.fail("添加失败");
    }

    @Operation(summary = "根据id修改设施部件信息")
    @PostMapping("/updateInfo/{id}")
    public Result<String> updateInfo(@RequestBody ComponentsAddDto componentsDto, @PathVariable("id") String id) {
        Components components = new Components();
        BeanUtils.copyProperties(componentsDto, components);
        components.setId(id);
        boolean update = componentsService.updateById(components);
        if (update) {
            return ResultUtil.success("修改成功");
        }
        return ResultUtil.fail("修改失败");
    }

    @Operation(summary = "根据id删除设施部件信息")
    @GetMapping("/deleteById")
    public Result<String> deleteById(@RequestParam("id") String id) {
        boolean remove = componentsService.removeById(id);
        if (remove) {
            return ResultUtil.success("删除成功");
        }
        return ResultUtil.fail("删除失败");
    }

    @Operation(summary = "根据id查询设施部件信息")
    @GetMapping("/getById")
    public Result<List<ComponentsListVo>> getById(@RequestParam("id") String id) {
        MPJLambdaWrapper<Components> wrapper = componentsService.getMPJWrapper().eq(Components::getId,id);
        List<ComponentsListVo> componentsListVos = componentsService.selectJoinList(ComponentsListVo.class, wrapper);
        return ResultUtil.success(componentsListVos);
    }

    @Parameter(name = "diseaseRegistrationId", description = "病害登记id(可为空)")
    @Operation(summary = "根据病害登记id查询设施部件信息列表")
    @GetMapping("/getList")
    public Result<Page<ComponentsListVo>> getList(@RequestParam("pageIndex") int pageIndex, @RequestParam("pageSize") int pageSize,
                                            @RequestParam(value = "diseaseRegistrationId",required = false) String diseaseRegistrationId) {

        MPJLambdaWrapper<Components> wrapper = componentsService.getMPJWrapper().apply("1=1")
                .eq( diseaseRegistrationId != null && !diseaseRegistrationId.isEmpty(), Components::getDiseaseRegistrationId, diseaseRegistrationId);

        Page<ComponentsListVo> componentsListVoPage = componentsService.selectJoinListPage(new Page<>(pageIndex, pageSize), ComponentsListVo.class, wrapper);
        return ResultUtil.success(componentsListVoPage);
    }

    @Parameter(name = "modelName", description = "型号名称")
    @PostMapping("/addModelNumInfo")
    @Operation(summary = "添加型号信息")
    public Result<String> addModelNumInfo(@RequestParam("modelName") String modelName) {

        LambdaQueryWrapper<ModelNum> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ModelNum::getModelName, modelName);
        List<ModelNum> modelNums = modelNumMapper.selectList(queryWrapper);
        if (!modelNums.isEmpty()) {
            return ResultUtil.fail("该型号已存在");
        }

        ModelNum modelNum = new ModelNum();
        modelNum.setModelName(modelName);
        int insert = modelNumMapper.insert(modelNum);
        if (insert > 0) {
            return ResultUtil.success("添加成功");
        }
        return ResultUtil.fail("添加失败");
    }

    @Parameter(name = "modelName", description = "型号名称")
    @PostMapping("/updateModelNumById/{id}")
    @Operation(summary = "根据id修改型号信息")
    public Result<String> updateById(@RequestParam("modelName") String modelName,
                                     @PathVariable("id") String id) {
        ModelNum modelNum = new ModelNum();
        modelNum.setId(id);
        modelNum.setModelName(modelName);
        int update = modelNumMapper.updateById(modelNum);
        if (update > 0) {
            return ResultUtil.success("修改成功");
        }
        return ResultUtil.fail("修改失败");
    }

    @GetMapping("/getModelNumById")
    @Operation(summary = "根据id获取型号信息")
    public Result<ModelNum> getModelNumById(@RequestParam("id") String id) {
        ModelNum modelNum = modelNumMapper.selectById(id);
        return ResultUtil.success(modelNum);
    }

    @GetMapping("/deleteModelNumById")
    @Operation(summary = "根据id删除型号信息")
    public Result<String> deleteModelNumById(@RequestParam("id") String id) {
        int delete = modelNumMapper.deleteById(id);
        if (delete > 0) {
            return ResultUtil.success("删除成功");
        }
        return ResultUtil.fail("删除失败");
    }


    @Parameter(name = "modelName", description = "型号名称(可为空)")
    @GetMapping("/getModelNumList")
    @Operation(summary = "获取型号列表")
    public Result<Page<ModelNum>> getModelNumList(@RequestParam("pageIndex") int pageIndex,
                                                  @RequestParam("pageSize") int pageSize,
                                                  @RequestParam(value = "modelName",required = false) String modelName) {
        LambdaQueryWrapper<ModelNum> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.apply("1=1").eq(modelName != null && !modelName.isEmpty(), ModelNum::getModelName, modelName);
        queryWrapper.orderByDesc(ModelNum::getPublishTime);
        Page<ModelNum> modelNumPage = modelNumMapper.selectPage(new Page<>(pageIndex, pageSize), queryWrapper);
        return ResultUtil.success(modelNumPage);
    }

    @PostMapping("/addPartsInfo")
    @Operation(summary = "添加零部件信息")
    public Result<String> addPartsInfo(@RequestBody PartsDto partsDto) {

        LambdaQueryWrapper<Parts> queryWrapper = new LambdaQueryWrapper<Parts>()
                .eq(Parts::getPartsName, partsDto.getPartsName());
        List<Parts> parts1 = partsMapper.selectList(queryWrapper);
        if (!parts1.isEmpty()) {
            return ResultUtil.fail("零部件名称已存在");
        }

        Parts parts = new Parts();
        BeanUtils.copyProperties(partsDto, parts);
        int insert = partsMapper.insert(parts);
        if (insert > 0) {
            return ResultUtil.success("添加成功");
        }
        return ResultUtil.fail("添加失败");
    }

    @Parameter(name = "partsName", description = "零部件名称")
    @PostMapping("/updatePartsInfoById/{id}")
    @Operation(summary = "根据id修改零部件信息")
    public Result<String> updatePartsInfoById(@RequestBody PartsDto partsDto,@PathVariable("id") String id) {
        Parts parts = new Parts();
        BeanUtils.copyProperties(partsDto, parts);
        parts.setId(id);
        int update = partsMapper.updateById(parts);
        if (update > 0) {
            return ResultUtil.success("修改成功");
        }
        return ResultUtil.fail("修改失败");
    }

    @GetMapping("/deletePartsInfoById")
    @Operation(summary = "根据id删除零部件信息")
    public Result<String> deletePartsInfoById(@RequestParam("id") String id) {
        int delete = partsMapper.deleteById(id);
        if (delete > 0) {
            return ResultUtil.success("删除成功");
        }
        return ResultUtil.fail("删除失败");
    }

    @GetMapping("/getPartsInfoById")
    @Operation(summary = "根据id查询零部件信息")
    public Result<Parts> getPartsInfo(@RequestParam("id") String id){
        Parts parts = partsMapper.selectById(id);
        return ResultUtil.success(parts);
    }

    @Parameter(name = "partsName", description = "零部件名称(可为空)")
    @GetMapping("/getPartsInfoList")
    @Operation(summary = "查询零部件信息列表")
    public Result<Page<Parts>> getPartsInfoList(@RequestParam("pageIndex") int pageIndex,
                                              @RequestParam("pageSize") int pageSize,
                                                @RequestParam(value = "partsName",required = false) String partsName) {
        LambdaQueryWrapper<Parts> queryWrapper = new LambdaQueryWrapper<Parts>()
                .apply("1=1")
                .eq(partsName != null && !partsName.isEmpty(),Parts::getPartsName, partsName)
                .orderByDesc(Parts::getPublishTime);

        Page<Parts> partsPage = partsMapper.selectPage(new Page<>(pageIndex, pageSize), queryWrapper);
        return ResultUtil.success(partsPage);
    }


}
